package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.persistence.dao.FulltimeTrainRuleMapper;
import com.stylefeng.guns.common.persistence.model.Employee;
import com.stylefeng.guns.common.persistence.model.FulltimeTrainPoints;
import com.stylefeng.guns.common.persistence.model.FulltimeTrainRule;
import com.stylefeng.guns.common.persistence.service.impl.PointServiceImpl;
import com.stylefeng.guns.modular.system.service.IFulltimeTrainRuleService;
import com.stylefeng.guns.modular.train.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网络培训积分 服务实现类
 * </p>
 *
 * @author jondai
 * @since 2017-12-27
 */
@Service
public class FulltimeTrainRuleServiceImpl extends PointServiceImpl<FulltimeTrainRuleMapper, FulltimeTrainRule> implements IFulltimeTrainRuleService {

    @Autowired
    private FulltimeTrainRuleMapper fulltimeTrainRuleMapper;

    @Autowired
    private IEmployeeService employeeService;
    @Override
    public void empty() {
        fulltimeTrainRuleMapper.empty();
    }

    @Override
    public List<FulltimeTrainRule> selectAll() {
        return fulltimeTrainRuleMapper.selectAll();
    }


    /**
     * 根据学时计算积分
     * 计算规则：j1积分到j2积分之间是每学时是ss分
     * 方法：1.获取区间规则
     * @param hours
     * @return
     */
    @Override
    public Double integralCal(Double hours, Integer empType) {
        Double integral = 0.0;

        if(hours == null || hours < 0.0){
            integral = 0.0;
        }else{
            integral = getaDouble(hours, empType, integral);
        }

        return integral;
    }

    public Double getaDouble(Double hours, Integer empType, Double integral) {
        FulltimeTrainRule fulltimeTrainRule = new FulltimeTrainRule();
        fulltimeTrainRule.setEmpType(empType);
        EntityWrapper<FulltimeTrainRule> wrapper = new EntityWrapper<>();
        wrapper.setEntity(fulltimeTrainRule);
        wrapper.orderBy("end_hour");

        List<FulltimeTrainRule> fulltimeTrainRules = this.selectList(wrapper);
        for (FulltimeTrainRule rule : fulltimeTrainRules) {
            if(hours >= rule.getEndHour()){
                hours = hours - rule.getEndHour();
                integral += rule.getEndHour() * rule.getCredit();
            }else{
                integral += hours * rule.getCredit();
                break;
            }
        }
        return integral;
    }

    public Double integralCal(Double hours, Integer empType,Map<Integer,List<FulltimeTrainRule>> fulltimeTrainRulesMap) {
        Double integral = 0.0;

        if(hours == null || hours < 0.0){
            integral = 0.0;
        }else{
            List<FulltimeTrainRule> fulltimeTrainRules = fulltimeTrainRulesMap.get(empType);
            if(fulltimeTrainRules == null || fulltimeTrainRules.size() < 1){
                integral = 0.0;
            }else{
                Comparator<FulltimeTrainRule> comparator = Comparator.comparing(FulltimeTrainRule::getEndHour);
                fulltimeTrainRules.sort(comparator);
                for (FulltimeTrainRule rule : fulltimeTrainRules) {
                    if(hours >= rule.getEndHour()){
                        hours = hours - rule.getEndHour();
                        integral += rule.getEndHour() * rule.getCredit();
                    }else{
                        integral += hours * rule.getCredit();
                        break;
                    }
                }
            }

        }

        return integral;
    }

    @Override
    public void integralCal(FulltimeTrainPoints fulltimeTrainPoints) throws Exception {
        String empCode = fulltimeTrainPoints.getEmpCode();
        Employee employee = employeeService.selectByCode(empCode);

        if(employee == null){
            System.out.println("无"+fulltimeTrainPoints.getEmpName()+"员工!");
            fulltimeTrainPoints.setCredit(0.0);
//            throw new Exception("无"+fulltimeTrainPoints.getEmpName()+"员工!" );
        }else{
            Integer empType = employee.getPostType() == null ? 1 : Integer.parseInt(employee.getPostType());
            Double aDouble = integralCal(fulltimeTrainPoints.getFinishHours(), empType);
            fulltimeTrainPoints.setCredit(aDouble);
        }
    }

    @Override
    public void integralCal(FulltimeTrainPoints fulltimeTrainPoints, Map<Integer, List<FulltimeTrainRule>> map) throws Exception {
        String empCode = fulltimeTrainPoints.getEmpCode();
        Employee employee = employeeService.selectByCode(empCode);

        if(employee == null){
            System.out.println("无"+fulltimeTrainPoints.getEmpName()+"员工!");
            fulltimeTrainPoints.setCredit(0.0);
//            throw new Exception("无"+fulltimeTrainPoints.getEmpName()+"员工!" );
        }else{
            if("04 不合格".equals(fulltimeTrainPoints.getCheckResult())){
                fulltimeTrainPoints.setCredit(0.0);
            }else{
                Integer empType = employee.getPostType() == null ? 1 : Integer.parseInt(employee.getPostType());
                Double aDouble = integralCal(fulltimeTrainPoints.getFinishHours(), empType,map);
                fulltimeTrainPoints.setCredit(aDouble);
            }

        }
    }

}
